<?php
	function set_message($message, $type, $location){
		global $login_message;

		if($location == 1)
			echo "<div id='message' class='".$type."'><span></span><a>".$message."</a></div>";
		else
			$login_message = "<div id='message' class='".$type."'><span></span><a>".$message."</a></div>";
	}
	
	function md5crypt($password,$salt){
		if(!$salt){
			// create a salt that ensures crypt creates an md5 hash
			$base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
							.'abcdefghijklmnopqrstuvwxyz0123456789+/';
			$salt='$1$';
			for($i=0; $i<8; $i++){
				$salt.=$base64_alphabet[rand(0,63)];
			}
			$salt .= '$';
		}
		
		// return the crypt md5 password
		return crypt($password,$salt);
	}
	
	if(isset($_POST['sbmt_login'])){
		$username = $_POST['username'];
		$password = $_POST['password'];
		
		if(!strstr($username,"@".ISLAND_PREFIX.""))
			$username = "".$username."@".ISLAND_PREFIX."";
				
		$ldap_connection = ldap_connect(HOST_LDAP,PORT_LDAP) or die("Error connecting to the LDAP server");
		
		ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
		ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
			
		if ($ldap_connection) {
			$search_for = "uid=".$username."";
			
			$ldap_bind = ldap_bind($ldap_connection, USER_LDAP, PASS_LDAP);
			
			$search = ldap_search($ldap_connection, "".LDAP_PEOPLE.",".LDAP_TREE."", $search_for);
			
			if(ldap_count_entries($ldap_connection, $search)){
				$ldapResults = ldap_get_entries($ldap_connection, $search);
				$crypted_password = '{CRYPT}'.md5crypt($password, substr($ldapResults[0]['userpassword'][0],7,12)).'';
				
				$password_verify = ldap_compare($ldap_connection, "".$search_for.",".LDAP_PEOPLE.",".LDAP_TREE."", 'userpassword', $crypted_password);
				$userEnable_verify = ldap_compare($ldap_connection, "".$search_for.",".LDAP_PEOPLE.",".LDAP_TREE."", 'userEnable', 'TRUE');
					
				if($password_verify != 1)
					set_message('Invalid User Name, Password or institution.','error',0);
				else if($userEnable_verify != 1)
					set_message('Account not approved yet. Please, contact your testbed administrator.','alert',0);
				else{
					$_SESSION[''.SECURITY_SESSION.''] = array(
															'username' => $username,
															'admin' => $ldapResults[0]['omfadmin'][0]
														);
					Header ('Location: index.php?page=reservation');
				}
			}else
				set_message('Invalid User Name, Password or institution.','error',0);		
		}
	}
	
	function validating_authentication($type){
		if($type == 'admin'){
			if($_SESSION[''.SECURITY_SESSION.'']['admin'] != 'TRUE')
				Header('Location: index.php');
		}
		
		if(!$_SESSION[''.SECURITY_SESSION.''])
			Header('Location: index.php');
	}
	
	function send_mail($sender, $receiver, $subject, $message){
		require("phpmailer/class.phpmailer.php");

		$mail = new PHPMailer();

		$mail->IsSMTP();
		$mail->Host = "localhost";
		$mail->From = $sender['email'];
		$mail->FromName = $sender['name'];
		
		for($i = 0; $i < count($receiver); $i++)
			$mail->AddAddress($receiver[$i]['email'], $receiver[$i]['name']);
			
		$mail->IsHTML(true);
		$mail->CharSet = 'iso-8859-1';
		$mail->Subject  = $subject;
		$mail->Body = $message;

		$send = $mail->Send();

		$mail->ClearAllRecipients();
		$mail->ClearAttachments();

		if($send)
			return TRUE;
		else
			return FALSE;
	}
	
	function mk_drawCalendar($m,$y){
		if ((!$m) || (!$y)){ 
			$m = date("m",mktime());
			$y = date("Y",mktime());
		}

		/*== get what weekday the first is on ==*/
		$tmpd = getdate(mktime(0,0,0,$m,1,$y));
		$month = $tmpd["month"]; 
		$firstwday= $tmpd["wday"];

		$lastday = mk_getLastDayofMonth($m,$y);
		$formaction = "index.php?page=reservation";

		?>
	
		<table cellpadding="2" cellspacing="0" border="1">
		<tr><td colspan="7" bgcolor="#CCCCDD">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr><th width="20"><a href="<?php echo $formaction; ?>&m=<?php $a=(($m-1)<1) ? 12 : $m-1; echo $a; ?>&y=<?php $b=(($m-1)<1) ? $y-1 : $y; echo $b; ?>">&lt;&lt;</a></th>
			<th><div align="center"><?php echo "$month"; ?></div></th>
			<th width="20"><a href="<?php echo $formaction; ?>&m=<?php $c=(($m+1)>12) ? 1 : $m+1; echo $c; ?>&y=<?php $d=(($m+1)>12) ? $y+1 : $y; echo $d; ?>">&gt;&gt;</a></th>
			</tr></table>
		</td></tr>

		<tr><th width=30 class="tcell">Sun</th><th width=30 class="tcell">Mon</th>
			<th width=30 class="tcell">Tue</th><th width=30 class="tcell">Wen</th>
			<th width=30 class="tcell">Thu</th><th width=30 class="tcell">Fri</th>
			<th width=30 class="tcell">Sat</th></tr>

		<?php $d = 1;
			$wday = $firstwday;
			$firstweek = true;

			/*== loop through all the days of the month ==*/
			while ( $d <= $lastday) {

				/*== set up blank days for first week ==*/
				if ($firstweek) {
					echo "<tr>";
					for ($i=1; $i<=$firstwday; $i++) 
					{ echo "<td><font size=2>&nbsp;</font></td>"; }
					$firstweek = false;
				}

				/*== Sunday start week with <tr> ==*/
				if ($wday==0) { echo "<tr>"; }

				/*== check for event ==*/  
				echo "<td class='tcell'>";
				if(strlen($m)<2) { $m= "0" . $m; }
				if(strlen($d)<2) { $d= "0" . $d; }
				/* href=\"$formaction&m=$m&y=$y#\" */
				/* href=\"#calendar\" */
				echo "<a href=\"#calendar\" onClick=\"document.f.start_date.value='$y-$m-$d';\">$d</a>";
				echo "</td>\n";

				/*== Saturday end week with </tr> ==*/
				if ($wday==6) { echo "</tr>\n"; }

				$wday++;
				$wday = $wday % 7;
				$d++;
			}
		?>
		</tr></table>
	<?php
	}

	function mk_getLastDayofMonth($mon,$year){
		for ($tday=28; $tday <= 31; $tday++){
			$tdate = getdate(mktime(0,0,0,$mon,$tday,$year));
			if ($tdate["mon"] != $mon) 
			{ break; }

		}
		$tday--;

		return $tday;
	}

?>
